package smdc.system.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import smdc.common.utils.StringUtils;

/**
 * 用于处理json数据的工具类
 *
 * @author maorui
 * @date Created in 2024/9/13 13:58
 **/
public class JsonUtil {

    //帮我写一个用于json格式化的工具类，并且可以美化
    public static String formatJson(String query) {
        try {
            if (StringUtils.isEmpty(query)) {
                return "";
            }
            ObjectMapper mapper = new ObjectMapper();
            Object jsonObj = mapper.readValue(query, Object.class);
            return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObj);
        } catch (Exception e) {
            return query;
        }
    }
    public static void main(String[] args) {
        String str = "{\"name\":\"张三\",\"age\":20}";
        String s = formatJson(str);
        System.out.println(s);
    }
}
